﻿using System;
using System.Threading.Tasks;
using FortuneUniverse.Fortune.Algorithms;

namespace FortuneUniverse.Fortune.Dyson
{
  public class PlanetAlgorithm5 : PlanetAlgorithm
  {
    public override void GenerateTerrain(double modX, double modY)
    {
      URandom1 urandom1 = new URandom1(this.planet.seed);
      int seed1 = urandom1.Next();
      int seed2 = urandom1.Next();
      SimplexNoise simplexNoise = new SimplexNoise(seed1);
      SimplexNoise simplexNoise2 = new SimplexNoise(seed2);
      PlanetRawData data = this.planet.data;
      Parallel.For(0, data.dataLength, (Action<int>) (i =>
      {
        double num1 = (double) data.vertices[i].x * (double) this.planet.radius;
        double num2 = (double) data.vertices[i].y * (double) this.planet.radius;
        double num3 = (double) data.vertices[i].z * (double) this.planet.radius;
        double num4 = 0.0;
        double num5 = Maths.Levelize(num1 * 0.007);
        double num6 = Maths.Levelize(num2 * 0.007);
        double num7 = Maths.Levelize(num3 * 0.007);
        double xin = num5 + simplexNoise.Noise(num1 * 0.05, num2 * 0.05, num3 * 0.05) * 0.04;
        double yin = num6 + simplexNoise.Noise(num2 * 0.05, num3 * 0.05, num1 * 0.05) * 0.04;
        double zin = num7 + simplexNoise.Noise(num3 * 0.05, num1 * 0.05, num2 * 0.05) * 0.04;
        double num8 = Math.Abs(simplexNoise2.Noise(xin, yin, zin));
        double num9 = (0.16 - num8) * 10.0;
        double num10 = num9 <= 0.0 ? 0.0 : (num9 <= 1.0 ? num9 : 1.0);
        double num11 = num10 * num10;
        double num12 = (simplexNoise.Noise3DFBM(num2 * 0.005, num3 * 0.005, num1 * 0.005, 4) + 0.22) * 5.0;
        double num13 = num12 <= 0.0 ? 0.0 : (num12 <= 1.0 ? num12 : 1.0);
        double num14 = Math.Abs(simplexNoise2.Noise3DFBM(xin * 1.5, yin * 1.5, zin * 1.5, 2));
        double num15 = num4 - num11 * 1.2 * num13;
        if (num15 >= 0.0)
          num15 += num8 * 0.25 + num14 * 0.6;
        double num16 = num15 - 0.1;
        double num17 = -0.3 - num16;
        if (num17 > 0.0)
        {
          double num18 = num17 <= 1.0 ? num17 : 1.0;
          num16 = -0.3 - (3.0 - num18 - num18) * num18 * num18 * 3.70000004768372;
        }
        data.heightData[i] = (ushort) (((double) this.planet.radius + num16 + 0.2) * 100.0);
      }));
    }
  }
}
